package com.desire.controller.sys;

import com.desire.bean.common.ResultBean;
import com.desire.bean.sys.Dict;
import com.desire.common.validation.group.Modify;
import com.desire.common.validation.group.Save;
import com.desire.controller.common.BaseController;
import com.desire.service.sys.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author LittleDesire
 * @description
 * @date 2022-05-18 周三 15:31
 * @since 17
 */
@RestController
@RequestMapping("/api/dict")
@Api(tags = "字典管理")
public class DictController extends BaseController {
    @Autowired
    DictService dictService;

    @PostMapping("/save")
    @ApiOperation(value = "新增字典", notes = "添加字典信息")
    public ResultBean save(@RequestBody @Validated({Save.class}) Dict dict) {
        return saveResult(dictService.save(dict));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改字典", notes = "修改字典信息")
    public ResultBean update(@RequestBody @Validated({Modify.class}) Dict dict) {
        return updateResult(dictService.update(dict));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除字典", notes = "逻辑删除字典信息")
    public ResultBean delete(String id) {
        int rs = dictService.delete(id);
        if (rs == 1) {
            return success("删除成功");
        } else if (rs == 2) {
            return fail("有子字典，请先删除子数据");
        } else {
            return fail("删除失败");
        }
    }

    @DeleteMapping("/deleteGroup")
    @ApiOperation(value = "删除字典树", notes = "逻辑删除字典树信息")
    public ResultBean deleteGroup(String code) {
        int rs = dictService.deleteGroup(code);
        return deleteResult(rs);
    }

    @PostMapping("/child")
    @ApiOperation(value = "查询子字典列表", notes = "查询字典列表信息")
    public ResultBean queryChild(@RequestBody Map<String, Object> map) {
        return success("字典列表", dictService.queryChild(map));
    }

    @GetMapping("/get")
    @ApiOperation(value = "根据父id查询子字典列表", notes = "树")
    public ResultBean queryChildByParentId(String pid) {
        return success("子字典列表", dictService.queryChildByParentId(pid));
    }

    @PostMapping("/children")
    @ApiOperation(value = "查询供应商", notes = "操作信息")
    public ResultBean queryChildren(@RequestBody Map<String, Object> map) {
        List<Map<String, Object>> maps = dictService.queryChildren(map);
        return success("子数据 ", maps);
    }
}
